package com.lancoo.answer.helper;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.lancoo.answer.R;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.impl.LoadingPopupView;

/**
 * Created by pengmi on 2021/12/9 19:50
 * Description:加载中弹窗
 */
public class LoadDialogHelper {


    public static AlertDialog showLoadDialog(Context mContext,int resID,boolean isCancel){
        View inflater=LayoutInflater.from(mContext).inflate(R.layout.ev_dialog_loading,null);
        AlertDialog.Builder builder=new AlertDialog.Builder(mContext, R.style.ev_dialogStyle);
        builder.setView(inflater);
        builder.setCancelable(isCancel);
        AlertDialog alertDialog = builder.create();
        if(!(mContext instanceof Activity)||!((Activity)mContext).isFinishing()){
            alertDialog.show();
        }
        if(resID!=0){
            TextView textView=inflater.findViewById(R.id.tv_load);
            textView.setText(resID);
        }else{
            inflater.findViewById(R.id.tv_load).setVisibility(View.GONE);
        }
        Window window = alertDialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.dimAmount=0.1f;
        window.setAttributes(attributes);
        ProgressBar progressBar = inflater.findViewById(R.id.img_load);
        progressBar.setIndeterminateDrawable(mContext.getResources().getDrawable(R.drawable.ev_loading_animation));
        return alertDialog;
    }

    public static BasePopupView showLoadPopup(Context mContext, int resID, boolean isCancel){
        return new XPopup.Builder().dismissOnTouchOutside(false)
                .dismissOnBackPressed(isCancel)
                .popupAnimation(PopupAnimation.NoAnimation)
                .hasShadowBg(false).asCustom(new LoadingPopupView(
                        mContext, 0,resID))
                .show();
    }


}
